package com.cvo.scrumtoolkit.client.pannels.widgets;

import java.util.LinkedList;
import java.util.List;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class BacklogWidget extends Composite {
	private BacklogItem backupBacklogItem;
	private Project project;
	private Integer backupInteger;
	private BacklogItem backlogItem;
	private List<BacklogItem> backlogItems;
	private static BacklogWidgetUiBinder uiBinder = GWT
			.create(BacklogWidgetUiBinder.class);

	interface BacklogWidgetUiBinder extends UiBinder<Widget, BacklogWidget> {
	}
	public BacklogWidget(BacklogItem backlogItem, List<BacklogItem> backlogItems, Project project) {
		initWidget(uiBinder.createAndBindUi(this));
		this.project=project;
		this.backlogItems = backlogItems;
		this.backlogItem= backlogItem;
		lblBacklogname.setText(this.backlogItem.getBacklogItemName());
		lblPrio.setTitle("Prioriteit");
		lblPrio.setText(backlogItems.indexOf(backlogItem)+1+"");
		Controle();

	}
	public void Switch(int i){
		backupBacklogItem=backlogItem;
		backupInteger=backlogItems.indexOf(backlogItem)-i;
		backlogItems.set(backlogItems.indexOf(backlogItem), backlogItems.get(backlogItems.indexOf(backlogItem)-i));
		backlogItems.set(backupInteger, backupBacklogItem);
		try {
			ControllerImpl.getController().changeView(View.PROJECTBACKLOG, project.getId().toString(), null);
		} catch (Exception ex) {
			// TODO Auto-generated catch block
		}	
	}
	public void Controle(){
		if(backlogItems.size()-1==backlogItems.indexOf(backlogItem)){
			buttonmin.setEnabled(false);
		}
		if(backlogItems.indexOf(backlogItem)==0){
			buttonplus.setEnabled(false);
		}
	}
	
	@UiField 
	Label lblBacklogname;
	
	@UiField
	Label lblPrio;
		
	@UiField
	Button buttonplus;
	
	@UiField
	Button buttonmin;
	
	@UiHandler("buttonplus")
	void onPlus(ClickEvent e) {
		Switch(1);
	}
	
	@UiHandler("buttonmin")
	void onMin(ClickEvent e) {
		Switch(-1);
	}
	
	
	
	

}
